package Calculator;
import Calculator.*;
import Calculator.Stack.*;
public class Nbl {
   private Linklist s1=new Linklist();//s1用于存储运算符
   private Linklist s2=new Linklist();//s2用于存储波兰式
   private Prior_Judgement s3=new Prior_Judgement();
   private Linklist s4=new Linklist();//s4用于保存逆序输出的逆波兰式
   private Linklist s5=new Linklist();//s5用于计算逆波兰式的值
   private NewClass_Nbl c=new NewClass_Nbl();//返回值为String型
   private NewClass_Nbl c1=new NewClass_Nbl();
   private String info;
   private Test_calculator a=new Test_calculator();
   public  String getNbl(String str1){
       String str=a.getMid(str1);
       int count=0;
       int i=0;
       info="";
       s1.push("#");
       String info1=c1.getNumber(str);
       while(!info1.equals("error")){
           count++;
           info1=c1.getNumber(str);
       }
       while(i<count){
       info=c.getNumber(str);
       if(!info.equals("error")&&!info.equals("(")&&!info.equals(")")&&!info.equals("+")&&!info.equals("-")&&!info.equals("*")&&!info.equals("/")&&!info.equals("%"))
           s2.push(info);
       else if(!info.equals("error")&&info.equals("("))
           s1.push(info);
       else if(!s1.isEmpty()&&!info.equals("error")&&info.equals(")")){
       String temp1=(String)s1.getTop();
       while(!temp1.equals("(")){
           s2.push((String)s1.pop());
           if(!s1.isEmpty())
             temp1=(String)s1.getTop();
          }
       s1.pop();
        }
       else if(!info.equals("error")&&!info.equals("(")&&!info.equals(")")){
           String temp2=(String)s1.getTop();
           if(temp2.equals("("))
               s1.push(info);
           else{
               if(s3.getPrior(info)>s3.getPrior(temp2))
                   s1.push(info);
               else{
                 while(s3.getPrior(temp2)>=s3.getPrior(info)&&!temp2.equals("(")){
                    s2.push((String)s1.pop());
                    if(!s1.isEmpty())
                     temp2=(String)s1.getTop();
               }
                 s1.push(info);
         }
       }
   }
       i++;
       }
       String temp3=(String)s1.pop();
       while(!s1.isEmpty()&&!temp3.equals("#")){
           s2.push(temp3);
           temp3=(String)s1.pop();
       }
       while(!s2.isEmpty()){
           s4.push(s2.pop());
       }
       s5.push("#");
       while(!s4.isEmpty()){//有瑕疵
           String info2="";
           String info3="";
           String info4="";
           info2=(String)s4.pop();
           if(!info2.equals("+")&&!info2.equals("-")&&!info2.equals("*")&&!info2.equals("/")&&!info2.equals("%"))
               s5.push(info2);
           else{
               double a=0;double b=0;double c=0;
               if(!s5.isEmpty())
                   info3=(String)s5.pop();
               if(!s5.isEmpty()&&!s5.getTop().equals("#")){
                   info4=(String)s5.pop();
                   b=Double.parseDouble(info4);//此处有bug
               }
               a=Double.parseDouble(info3);//此处有bug
               switch(info2){
                   case "+":c=b+a;s5.push(String.valueOf(c));break;
                   case "-":c=b-a;s5.push(String.valueOf(c));break;
                   case "*":c=b*a;s5.push(String.valueOf(c));break;
                   case "/":c=b/a;s5.push(String.valueOf(c));break;
                   case "%":c=b%a;s5.push(String.valueOf(c));break;
                   default:break;
               }
           }
       }
       String result="";result=(String)s5.getTop();
       while(!s5.isEmpty())
           s5.pop();
       c1.getEnd(1);
       c.getEnd(1);
       return result;
   }
}

 

